package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.mapper.BHl7MoveDetailMapper;
import top.tangyh.lamp.care.service.BHl7MoveDetailService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BHl7MoveDetailManager;
import top.tangyh.lamp.care.entity.BHl7MoveDetail;
import top.tangyh.lamp.care.vo.result.MoveInfoOutVO;
import top.tangyh.lamp.care.vo.save.BHl7MoveDetailSaveVO;
import top.tangyh.lamp.care.vo.save.MoveInfoInVO;
import top.tangyh.lamp.care.vo.update.BHl7MoveDetailUpdateVO;
import top.tangyh.lamp.care.vo.result.BHl7MoveDetailResultVO;
import top.tangyh.lamp.care.vo.query.BHl7MoveDetailPageQuery;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static top.tangyh.lamp.common.constant.CommonConstant.YES_NUMBER;

/**
 * <p>
 * 业务实现类
 * 归转记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-15 13:49:07
 * @create [2024-07-15 13:49:07] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BHl7MoveDetailServiceImpl extends SuperServiceImpl<BHl7MoveDetailManager, Long, BHl7MoveDetail> implements BHl7MoveDetailService {


    private final BHl7MoveDetailMapper bHl7MoveDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMoveInfo(MoveInfoInVO moveInfoInVO) {
        BHl7MoveDetail moveDetailPO = new BHl7MoveDetail();
        moveDetailPO.setPatientIdent(moveInfoInVO.getPatientIdent());
        moveDetailPO.setHospitalNumber(moveInfoInVO.getHospitalNumber());
        if(null!=moveInfoInVO.getZrTime()){
            moveDetailPO.setZrTime(PDateUtils.getDateTimeOfTimestamp(moveInfoInVO.getZrTime()));
        }

        if (moveInfoInVO.getZcTime() != null) {
            moveDetailPO.setZcTime(PDateUtils.getDateTimeOfTimestamp(moveInfoInVO.getZcTime()));
        }
        moveDetailPO.setDeptIdent(moveInfoInVO.getDeptIdent());
        moveDetailPO.setDeptName(moveInfoInVO.getDeptName());
        moveDetailPO.setOldDeptIdent(moveInfoInVO.getOldDeptIdent());
        moveDetailPO.setOldDeptName(moveInfoInVO.getOldDeptName());
        moveDetailPO.setIsNotPlan(moveInfoInVO.getNotPlan());
        moveDetailPO.setIsDiy(YES_NUMBER);
        bHl7MoveDetailMapper.insert(moveDetailPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoveInfo(MoveInfoInVO moveInfoInVO) {
        BHl7MoveDetail moveDetailPO = bHl7MoveDetailMapper.selectById(moveInfoInVO.getId());
        if (moveDetailPO != null) {
            BHl7MoveDetail updateMoveDetailPO = new BHl7MoveDetail();
            updateMoveDetailPO.setId(moveInfoInVO.getId());
            updateMoveDetailPO.setZrTime(PDateUtils.getDateTimeOfTimestamp(moveInfoInVO.getZrTime()));
            updateMoveDetailPO.setDeptIdent(moveInfoInVO.getDeptIdent());
            updateMoveDetailPO.setDeptName(moveInfoInVO.getDeptName());
            updateMoveDetailPO.setOldDeptIdent(moveInfoInVO.getOldDeptIdent());
            updateMoveDetailPO.setOldDeptName(moveInfoInVO.getOldDeptName());
            if (moveInfoInVO.getZcTime() != null) {
                updateMoveDetailPO.setZcTime(PDateUtils.getDateTimeOfTimestamp(moveInfoInVO.getZcTime()));
            } else {
                updateMoveDetailPO.setZcTime(null);
            }
            updateMoveDetailPO.setIsNotPlan(moveInfoInVO.getNotPlan());
            bHl7MoveDetailMapper.updateById(updateMoveDetailPO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMoveInfo(Long id) {
        BHl7MoveDetail moveDetailPO = bHl7MoveDetailMapper.selectById(id);
        if (moveDetailPO != null) {
            BHl7MoveDetail delMoveDetailPO = new BHl7MoveDetail();
            delMoveDetailPO.setId(id);
            delMoveDetailPO.setIsDeleted(YES_NUMBER);
            bHl7MoveDetailMapper.updateById(delMoveDetailPO);
        }
    }

    @Override
    public List<MoveInfoOutVO> listMoveInfoInVO(String hospitalNumber) {

        List<MoveInfoOutVO> moveInfoOutVOList=new ArrayList<>();
        List<BHl7MoveDetail> list= bHl7MoveDetailMapper.selectListByHospitalNumber(hospitalNumber);
        for (int i = 0; i <list.size() ; i++) {
            BHl7MoveDetail bHl7MoveDetail=list.get(i);
            MoveInfoOutVO moveInfoOutVO = new MoveInfoOutVO();
          //  MoveInfoOutVO moveInfoOutVO= BeanUtil.copyProperties(bHl7MoveDetail,MoveInfoOutVO.class);
            BeanUtil.copyProperties(bHl7MoveDetail, moveInfoOutVO);
            moveInfoOutVO.setOldWardName(bHl7MoveDetail.getOldRoom());
            moveInfoOutVOList.add(moveInfoOutVO);
        }
       // List<MoveInfoOutVO> moveInfoOutVOList = .stream().map(MoveInfoOutVO::new).collect(Collectors.toList());

        return moveInfoOutVOList;
    }

    @Override
    public List<BHl7MoveDetailResultVO> selectRkListOfHistoryTb(String hospitalNumber) {
        return bHl7MoveDetailMapper.selectRkListOfHistoryTb(hospitalNumber);
    }

    @Override
    public BHl7MoveDetailResultVO selectCyTimeRecordByHospitalNumber(LocalDateTime rktime, String hospitalNumber) {
        return bHl7MoveDetailMapper.selectCyTimeRecordByHospitalNumber(rktime,hospitalNumber);
    }


}


